﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL;
using Model;

namespace DAL
{
    public class ProductType_Step : BaseRepository<ProductType_StepInfo>, IProductType_Step
    {
        #region 静态变量部分
        private const string PARM_STEP_ID = "@ID";
        private const string PARM_STEP_PRODUCTTYPEID = "@ProductTypeID";
        private const string PARM_STEP_STEPITEM = "@StepItem";
        private const string PARM_STEP_STEPINDEX = "@StepIndex";
        private const string PARM_STEP_POSTSTEP = "@PostStep";
        private const string PARM_STEP_NEXTSTEP = "@NextStep";

        private const string PARM_STEP_PRODUCTSN = "@ProductSN";
        private const string PARM_STEP_RESULT = "@result";
        #endregion

        #region 数据检索部分
        public IList<ProductType_StepInfo> LoadEntities()
        {
            return ExcuteSqlToGetEntities("ProductType_Step_select");
        }

        public ProductType_StepInfo LoadEntity<T1>(T1 id)
        {
            SqlParameter parm = GetParameter();
            parm.Value = id;
            return ExcuteSqlToGetEntity("ProductType_Step_selectbyID",parm);
        }

        public IList<ProductType_StepInfo> GetStepbyPID(string productTypeID)
        {
            SqlParameter parm = new SqlParameter(PARM_STEP_PRODUCTTYPEID, SqlDbType.NVarChar, 50);
            parm.Value = productTypeID;
            return ExcuteSqlToGetEntities("ProductType_Step_selectbyPID", parm);
        }

        public ProductType_StepInfo GetStepbyPIDItem(string productTypeID, string stepItem)
        {
            SqlParameter[] parms = GetPIDItemParameters();
            parms[0].Value = productTypeID;
            parms[1].Value = stepItem;
            return ExcuteSqlToGetEntity("ProductType_Step_selectbyPIDItem", parms);
        }
        #endregion

        #region 流程控制部分
        public bool ProductType_Step_HasValidSourceStep(string productSN, string productTypeID, string stepItem)
        {
            SqlParameter[] testParms = GetStepParameters();
            testParms[0].Value = productSN;
            testParms[1].Value = productTypeID;
            testParms[2].Value = stepItem;
            testParms[3].Direction = ParameterDirection.Output;

            bool isvalid = (bool)sqlHelper.ExecuteScalar(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "ProductType_Step_HasValidPostStep", testParms);
            //bool isvalid = (bool)testParms[3].Value;
            return isvalid;
        }

        #endregion

        #region 参数部分
        protected SqlParameter[] GetPIDItemParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_STEP_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_STEP_STEPITEM,SqlDbType.NVarChar,50)
                };
            return parms;
        }

        private static SqlParameter[] GetStepParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_STEP_PRODUCTSN,SqlDbType.NVarChar,50),
					new SqlParameter(PARM_STEP_PRODUCTTYPEID,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_STEP_STEPITEM,SqlDbType.NVarChar,50),
					new SqlParameter(PARM_STEP_RESULT,SqlDbType.Bit,1)
                };
            return parms;
        }
        #endregion

        #region 重写基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override ProductType_StepInfo GetEntityRecord(SqlDataReader rdr)
        {
            ProductType_StepInfo pro = new ProductType_StepInfo
            {
                ID = rdr.GetInt32(0),
                ProductTypeID = rdr.GetValue(1).ToString(),
                StepItem = rdr.GetValue(2).ToString(),
                StepIndex = rdr.GetInt32(3),
                PostStep = rdr.GetValue(4).ToString(),
                NextStep = rdr.GetValue(5).ToString()
            };
            return pro;
        }

        public override ProductType_StepInfo GetEntityRecord()
        {
            ProductType_StepInfo pro = new ProductType_StepInfo();
            return pro;
        }

        public override string GetInsertsp()
        {
            return "ProductType_Step_Insert";
        }

        public override string GetUpdatesp()
        {
            return "ProductType_Step_Update";
        }

        public override string GetDeletesp()
        {
            return "ProductType_Step_Delete";
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_STEP_ID, SqlDbType.Int, 4);
            return Parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_STEP_ID, SqlDbType.Int, 4),
					new SqlParameter(PARM_STEP_PRODUCTTYPEID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_STEP_STEPITEM,SqlDbType.NVarChar,50),
                    new SqlParameter(PARM_STEP_STEPINDEX, SqlDbType.Int, 4),
					new SqlParameter(PARM_STEP_POSTSTEP, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_STEP_NEXTSTEP,SqlDbType.NVarChar,50)
                };
            return parms;
        }

        public override void SetParameters(ProductType_StepInfo pro, params SqlParameter[] Parms)
        {
            Parms[0].Value = pro.ID;
            Parms[1].Value = pro.ProductTypeID;
            Parms[2].Value = pro.StepItem;
            Parms[3].Value = pro.StepIndex;
            Parms[4].Value = pro.PostStep;
            Parms[5].Value = pro.NextStep;
        }
        #endregion
    }
}
